﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.ServiceModel;
using BookClubIt.Processor;
using DTO = BookClubIt.DTO;

namespace BookClubIt
{
    public partial class bookshelf : BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                int start = 0;
                int.TryParse(Request.QueryString["start"], out start);
                if (start == 1 && BookClubProcessorInstance.IsAdmin(CurrentUserId, CID))
                {
                    ModalPopupExtender1.Show();

                }
                BindData();

            }
            else
            {
               // popupPanel.Visible = true;
                ModalPopupExtender1.Show();
            }

        }

        private void BindData()
        {
            //  Create a connection to the "pubs" database located 
            // on the local computer.

            hdnBid.Value = CID.ToString();
            DTO.BookClub club = BookClubProcessorInstance.GetBookClubById(CID);

            DTO.Books books = BookProcessorInstance.GetBooksForClub(CID);

         
            rptBookShelf.DataSource = books;
            rptBookShelf.DataBind();

            // Bind MyRepeater to the  DataSet. MyRepeater is the ID of the
            // Repeater control in the HTML section of the page.
            // lblName.Text = Repository.Instance.ReturnScore(ddlPlayer1.ToString(), ddlPlayer2.ToString()).Rows[0][0].ToString();
            // MyRepeater.DataSource = ds;
            //  MyRepeater.DataBind();

        
            lblClubName.Text = club.Name;

            hdnUid.Value = CurrentUserId.ToString();

            DTO.Book book = BookProcessorInstance.GetCurrentBook(CID);

            if (BookClubProcessorInstance.IsLeader(CurrentUserId, CID))
            {
                pnlBookSearch.Visible = true;
                divTrash.Visible = true;
                divDragInfo.Visible = true;
                btnOpenModal.Visible = true;
            }

        }
        public void BindData(DTO.Books books)
        {
            grdBookSearch.DataSource = books;
            grdBookSearch.DataBind();
        }
        protected void grdBookSearch_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
               DTO.Book book = e.Row.DataItem as DTO.Book;

             if (e.Row.RowType != DataControlRowType.DataRow)
                return;

             if (e.Row.RowType == DataControlRowType.DataRow)
             {
                 HyperLink lnkBookLink = ((HyperLink)e.Row.FindControl("lnkBookLink"));
                 LinkButton lnkAddToShelf = ((LinkButton)e.Row.FindControl("lnkAddToShelf"));
                 Image imgBookImage = ((Image)e.Row.FindControl("imgBookImage"));
                 Label lblBookTitle = ((Label)e.Row.FindControl("lblBookTitle"));
                 HiddenField hdnASIN = ((HiddenField)e.Row.FindControl("hdnASIN"));
                 Label lblBookAuthor = ((Label)e.Row.FindControl("lblBookAuthor"));
                 
                 lblBookTitle.Text = book.Title;
               //  lblBookAuthor.Text = "By: " + book.Author;
                 imgBookImage.ImageUrl = book.SmallImageURL;
                 lnkBookLink.NavigateUrl = book.AmazonURL;
                 hdnASIN.Value = book.ASIN;
                 lnkAddToShelf.CommandArgument = e.Row.RowIndex.ToString();

             }
        }
        protected void SearchBook_Click(object sender, EventArgs e)
        {
            lblSearchText.Text = "Search Results for '" + txtBook.Text + "'";
            Page.Title = "Search for " + txtBook.Text;
           string formattedSearch = txtBook.Text.Replace(" ", "%20");
           DTO.Books books = AmazonProcessorInstance.SearchBookByTitle(formattedSearch, "relevancerank");
          
            if (books == null)
            {
                return;
            }
            else
            {
                grdBookSearch.DataSource = books;
                grdBookSearch.DataBind();

            }
           // myModal.Style[HtmlTextWriterStyle.Display] = "block";
        }

        protected void grdBookSearch_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int rowNum = Convert.ToInt32(e.CommandArgument);
            HiddenField hdnASIN = (grdBookSearch.Rows[rowNum].FindControl("hdnASIN")) as HiddenField;

            if (e.CommandName == "AddToShelf")
            {
                if (hdnASIN != null)
                {
                    DTO.Books books = AmazonProcessorInstance.LookUpBookByASIN(hdnASIN.Value);
                    if (books.Count > 0)
                    {
                        foreach (DTO.Book book in books)
                        {
                            if (book != null)
                                BookProcessorInstance.SaveBook(book);
                                BookClubProcessorInstance.AddBookToClubShelf(CID, book.Id);
                                BindData();
                        }
                    }
                }

                
            }
        }

        protected void okbutton_Click(object sender, EventArgs e)
        {
            //Please write your code-behind code here
            ModalPopupExtender1.Hide();
        }
        
    }
}